// pages/shop-cart/shop-cart.js
const app = getApp()

const {globalData} = app


Component({
  behaviors: [],
  options: {
    pureDataPattern: /^_/, // 指定所有 _ 开头的数据字段为纯数据字段
  },
  properties: {
  },
  /**
   * 页面的初始数据
   */
  data: {
    productList: [],
    checkList: [],
    allCheck: false,
    totalPrice: 0
  },
  methods: {
    deleteHandle(event) {
      const {index} = event.currentTarget.dataset
      this.requestDelete(index)
    },
    onCheckChange(event) {
      const {index} = event.currentTarget.dataset
      let {productList} = this.data
      let data = productList[index]
      data.check = !data.check

      this.setData({
        totalPrice: this.countPrice(productList),
        allCheck: productList.every(item => item.check),
        [`productList[${index}]`]: data,
      })
    },
    // 数量改变
    countChange(event) {
      const {index} = event.currentTarget.dataset
      const count = event.detail
      this.requestEdit(index, count)
    },
    onAllCheckChange() {
      let {productList, allCheck} = this.data
      productList.forEach(item => {
        item.check = !allCheck
      })

      this.setData({
        allCheck: !allCheck,
        productList: productList,
        totalPrice: this.countPrice(productList)
      })
    },
    // 跳转去购买
    buyHandle() {
      let checkList = [], noCount = false

      this.data.productList.forEach(item => {
        if (item.check) {
          noCount = item.product_count <= 0

          checkList.push({
            specs_id: item.specs_id,
            merchant_id: item.merchant_id,
            product_count: item.product_count,
            product_id: item.product_id
          })
        }
      })
      if(noCount) {
        return wx.showToast({
          title: '请添加数量',
        })
      }

      if (checkList.length == 0) {
        return wx.showToast({
          title: '请选择商品',
        })
      }

      let listStr = encodeURIComponent(JSON.stringify(checkList))

      wx.navigateTo({
        url: `/pages/shop-list/shop-list?productList=${listStr}`,
      })
    },
    countPrice(data) {
      let price = 0
      data = data || this.data.productList
      data.forEach(item => {
        if(item.check) {
          price += item.product_total_price
        }
      })
      return price
    },
    // 设置产品数据
    setProductData(data = []) {
      data = JSON.parse(JSON.stringify(data))
      this.setData({
        totalPrice: this.countPrice(data),
        productList: data
      })
    },
    // 请求购物车数据
    requestShopSet() {
      app.requestShopCart((data) => {
        this.setProductData(data)
      })
    },
    // 请求删除购物车
    requestDelete(index) {
      let {productList} = this.data
      let data = productList[index]
      wx.ajax.post('deleteOrderCart.action', {id: data.id}, false).then(() => {
        productList.splice(index, 1)
        this.setProductData(productList)
        app.requestShopCart()
      })
    },
    // 请求编辑购物车
    requestEdit(index, count) {
      const {productList} = this.data
      let curData = productList[index]
      wx.ajax.post('editOrderCartById.action', {id: curData.id, product_count: count}, false).then(() => {
        curData.product_count = count
        curData.product_total_price = curData.product_sale_price * count
        this.setData({
          totalPrice: this.countPrice(productList),
          [`productList[${index}]`]: curData
        })
        app.requestShopCart()
      })
    },
    onLoad() {
      wx.hideShareMenu();
    },
    onShow() {
      if(!globalData.shopCart) {
        this.requestShopSet()
      } else {
        this.setProductData(globalData.shopCart)
      }
    }
  }
})